package ext.opensource.netty.server.example.websocket;

import java.util.Map;

import ext.opensource.netty.server.core.BaseServer;
import ext.opensource.netty.server.httpsocket.BaseWebSocketEvent;
import ext.opensource.netty.server.httpsocket.WebSocketSession;
import io.netty.buffer.ByteBuf;

public class WebSocketEventChat extends BaseWebSocketEvent {
	@Override
	public void onMessageStringEvent(final BaseServer sevice, final WebSocketSession session, final String msg) {
		final String msgStr = msg + " < " + session.id().asShortText();
		System.out.println("recv msg:" + msgStr);
		sevice.broadcastMessageString(msgStr);
	}

	@Override
	public void onMessageBinaryEvent(final BaseServer sevice, final WebSocketSession session, final ByteBuf msg) {
		session.sendBinaryMessage(msg);
	}

	@Override
	public void onOpenEvent(final BaseServer sevice, final WebSocketSession session,
			final Map<String, Object> parameter) {
		System.err.println("onOpenEvent parameter:" + parameter);
		sevice.broadcastMessageString("weclome " + session.channel().id().asShortText());
	}

	@Override
	public void onCloseEvent(final BaseServer sevice, final WebSocketSession ctx) {
		System.err.println("close");
	}
}
